![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
Note that the background is smoothly shaded only on computers using high color graphics. If your computer is using 256 color mode, the background will be covered with large bands of color. It will still have the same bad performance, however. Examine the code and figure out where it is spending all of its time. Use the Visual Basic Code Profiler (VBCP) if you like, or look at the timing results shown in Table 16.1. Then modify the code to improve the programs performance without introducing other bugs.
Option Explicit
Private Values(1 To 10) As Single
Generate some random data and draw the graph.
Private Sub Form_Load()
Load some random data.
LoadData
Shade the background.
ShadePicture
Draw the graph.
DrawGraph
End Sub
Create some random data.
Private Sub LoadData()
Dim i As Integer
Dim min As Single
Dim max As Single
Randomize
min = picGraph.ScaleHeight * 0.1
max = picGraph.ScaleHeight * 0.8
For i = 1 To 10
Values(i) = Int((max - min + 1) * Rnd + min)
Next i
End Sub
Shade picGraph from white at the top to
black at the bottom.
Private Sub ShadePicture()
Const START_COLOR = 255
Const END_COLOR = 0
Dim Y As Single
Dim clr As Single
Dim d_clr As Single
d_clr = (END_COLOR - START_COLOR) / picGraph.ScaleHeight clr = START_COLOR
For Y = 0 To picGraph.ScaleHeight
picGraph.Line (0, Y)- _
(picGraph.ScaleWidth, Y), _
RGB(clr, clr, clr)
clr = clr + d_clr
Next Y
End Sub
Draw the graph.
Private Sub DrawGraph()
Dim i As Integer
Dim dx As Single
Dim y0 As Single
y0 = picGraph.ScaleHeight * 0.9
dx = picGraph.ScaleWidth / 12
Draw the bar chart.
For i = 1 To 10
picGraph.Line (i * dx, y0)-((i + 1) * dx, Values(i)), vbRed, BF
Next i
For i = 1 To 10
picGraph.Line (i * dx, y0)-((i + 1) * dx, Values(i)), vbBlack, B
Next i
End Sub
Table 16.1 shows the function timing results given by VBCP for program Bad16 on a 133-megahertz Pentium. The table shows that program Bad16 spends almost all of its time in subroutine ShadePicture. Study these results and the code to see if you can figure out where the program is wasting so much time, but be warned: The solution does not lie entirely within the code shown here. If you are an experienced Visual Basic programmer, you should be able to deduce the problem. If you are a beginner, do not spend too much time looking at the code before you read the solution in Appendix A, Self-Test Solutions. SummaryProbably the most dangerous mistake programmers make when debugging is rushing through the job. They do not take the time to understand the context in which the code operates and they do not thoroughly test their changes. Treat repair code just as you treat new code and follow the same guidelines. Use good coding practices and test the results. The Bug Stoppers that follow summarize the other debugging practices covered in this chapter.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|